#!/usr/bin/env bash
#
# Copyright 2017-2018 Axel Huebl
#
# This file is part of PIConGPU.
#
# PIConGPU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PIConGPU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PIConGPU.
# If not, see <http://www.gnu.org/licenses/>.
#

# Disallow PRs to `ComputationalRadiationPhysics/picongpu` branch `master`
# if not an other mainline branch such as `dev` or `release-...`
#
# See: https://docs.travis-ci.com/user/environment-variables/
#      https://developer.github.com/v3/pulls/#get-a-single-pull-request
#
# -> only enforced for `master` branch
#    -> only enforced for mainline repo (not for forks)
#
# This file needs to be sourced in .travis.yml to work.
#
# @result 0 if correct target (or not in travis CI for mainline), else 1
#

# Are we even in travis? Otherwise pass this test.
if [ "$TRAVIS" != "true" ]
then
    echo "Not in travis, so I have nothing to do :)"
else

    mainline_slug="ComputationalRadiationPhysics/picongpu"

    # only enforced for PRs
    if [ "$TRAVIS_EVENT_TYPE" == "pull_request" ]
    then
        # only enforced for `master` branch
        if [ "$TRAVIS_BRANCH" == "master" ]
        then
            # only enforced for mainline repo (not for forks)
            if [ "$TRAVIS_REPO_SLUG" == "$mainline_slug" ]
            then
                # origin repo is not our mainline? so it's a PR from a fork!
                if [ "$TRAVIS_PULL_REQUEST_SLUG" != "$mainline_slug" ]
                then
                    # the PR came from a fork owned by the first part of the slug
                    pr_author=$(echo "$TRAVIS_PULL_REQUEST_SLUG" | awk -F "/" '{print $1}')
                    pr_branch=$TRAVIS_PULL_REQUEST_BRANCH
                    echo ""
                    echo "Pull request opened to wrong branch!"
                    echo ""
                    echo "New features need to go to our 'dev' branch but your"
                    echo "pull-request from '"$TRAVIS_PULL_REQUEST_SLUG"' was"
                    echo "sent to 'master' which is only updated by our"
                    echo "maintainers for new stable releases."
                    echo ""
                    echo "Please re-open your pull-request against our 'dev' branch:"
                    echo "  https://github.com/ComputationalRadiationPhysics/picongpu/compare/dev...$pr_author:$pr_branch?expand=1"
                    echo ""
                    echo "For further information, please see:"
                    echo "  https://github.com/ComputationalRadiationPhysics/picongpu/blob/dev/CONTRIBUTING.md"
                    exit 1
                fi
            fi
        fi
    fi
fi
